/*
 * @Description: 接送乘客（用户实时单） UserrealtimeorderProvide
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-09-03 15:14:16
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2019-12-23 17:38:50
 */
import 'package:flutter/material.dart';
import 'package:C20Driver/config/service_api.dart';
import 'package:C20Driver/model/get_trip_order_info.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/service/service_method.dart';

class UserrealtimeorderProvide with ChangeNotifier {
  String lastNumber = '0000'; // 乘客手机号
  String passengerIdNum = ''; // 乘客用户ID
  String orderId = ''; // 该笔订单ID

  /// 0 为去接乘客 1 为到达约定地点 2 为到达目的地
  int transferStatus = 0;

  String currentDisplayTarget; // 当前显示目标
  /// 当前前往目标位置
  var currentDestination = {};

  /// 路线规划起点
  var startingPoint = {};

  /// 起点名称
  String startingName;

  /// 路线规划终点
  var endLine = {};

  /// 目的地名称
  String destinationName;

  /// 当前我的位置
  var myLocation = {};

  /// 乘客当前位置
  var passengerPosition = {};

  /// 目的地位置
  var destinationLocation = {};

  // 修改订单状态
  void setTansferStatus(context, numData, data) {
    if (numData == 1) {
      DioUtils().request(ReqMethod.POST, ServiceApi.driverArriveBoardingPoint,
          params: {
            'order_id': orderId,
          }, success: (res) async {
        transferStatus = numData;
        notifyListeners();
      });
    } else if (numData == 2) {
      DioUtils().request(ReqMethod.POST, ServiceApi.receivePassenger, params: {
        'order_id': orderId,
      }, success: (res) async {
        transferStatus = numData;
        currentDisplayTarget = data.destinationName;
        currentDestination = destinationLocation;
        endLine = destinationLocation;
        NavigatorUtil.jump(context, '/pickUpPassengers');
        notifyListeners();
      });
    } else if (numData == 3) {
      DioUtils().request(ReqMethod.POST, ServiceApi.orderTripEnd, params: {
        'order_id': orderId,
        'dest_longitude': myLocation['longitude'],
        'dest_latitude': myLocation['latitude'],
        'dest_area': '下车点名称（1）'
      }, success: (res) async {
        transferStatus = 0;
        NavigatorUtil.jump(context,
            "/confirmedAmount?orderId=$orderId&sumCost=${res['cost']['sum_cost']}&startCost=${res['cost']['start_cost']}&timeCost=${res['cost']['time_cost']}&distanceCost=${res['cost']['distance_cost']}");
        notifyListeners();
      });
    }
    notifyListeners();
  }

  // 获取当前司机当前位置经纬度
  setMyLocation(longitude, latitude) {
    myLocation = {
      'latitude': latitude,
      'longitude': longitude,
    };
    startingPoint = {
      'latitude': latitude,
      'longitude': longitude,
    };
    notifyListeners();
  }

  // 设置乘客当前位置
  setPassengerPosition(context, data) {
    passengerPosition = {
      'latitude': double.parse(data.depLatitude),
      'longitude': double.parse(data.depLongitude)
    };
    endLine = {
      'latitude': double.parse(data.depLatitude),
      'longitude': double.parse(data.depLongitude)
    };

    currentDestination = {
      'latitude': double.parse(data.depLatitude),
      'longitude': double.parse(data.depLongitude)
    };

    lastNumber = data.lastNum; // 设置乘客手机号
    passengerIdNum = data.passengerId; // 乘客用户ID

    setStartPosition(context, data);
    notifyListeners();
  }

  // 设置起始位置
  setStartPosition(context, data) {
    startingName = data.depAre; // 起点名称
    destinationName = data.destArea; // 目的地名称
    orderId = data.orderId; // 设置订单ID

    currentDisplayTarget = data.depAre; // 当前显示目标名称
    currentDestination = {
      'latitude': double.parse(data.depLatitude),
      'longitude': double.parse(data.depLongitude)
    };
    destinationLocation = {
      'latitude': double.parse(data.destLatitude),
      'longitude': double.parse(data.destLongitude)
    }; // 设置目的地位置
    NavigatorUtil.jump(context, '/pickUpPassengers');
    notifyListeners();
  }

  setOrderId() {
    orderId = '';
    notifyListeners();
  }

  /// 司机获取行程中订单
  getOnTripOrderByDriverId() {
    DioUtils().request(ReqMethod.POST, ServiceApi.getOnTripOrderByDriverId,
        params: {}, success: (res) async {
      if (res['id'] != null && res['id'].length > 0) {
        orderId = res['id'];
        DioUtils()
            .request(ReqMethod.POST, ServiceApi.getTripOrderInfo, params: {
          'order_id': res['id'],
        }, success: (val) async {
          var getTripOrderInfo = GetTripOrderInfo.fromJson(val);

          switch (getTripOrderInfo.status) {
            case '0010':
              transferStatus = 0;
              break;
            case '0015': // 为去接乘客
              transferStatus = 1;
              break;
            case '0020': // 接到乘客,行程中
              transferStatus = 2;
              break;
            case '0030': // 为到达目的地
              transferStatus = 2;
              break;
            default:
          }

          lastNumber = getTripOrderInfo.lastNum; // 设置乘客手机号

          // 起点名称
          startingName = getTripOrderInfo.depAre;
          // 路线规划起点
          startingPoint = {
            'latitude': getTripOrderInfo.depLatitude,
            'longitude': getTripOrderInfo.depLongitude,
          };
          // 终点名称
          destinationName = getTripOrderInfo.destArea;
          // 路线规划终点
          endLine = {
            'latitude': getTripOrderInfo.destLatitude,
            'longitude': getTripOrderInfo.destLongitude,
          };

          // print('获取信息1111');
          // print(getTripOrderInfo.status.toString());
          // ToastX.center(getTripOrderInfo.status.toString());

          if (getTripOrderInfo.status == '0010') {
            // 当前现实目标名称
            currentDisplayTarget = getTripOrderInfo.depAre;
            // 当前现实目标位置
            currentDestination = {
              'latitude': getTripOrderInfo.depLatitude,
              'longitude': getTripOrderInfo.depLongitude,
            };
          } else if (getTripOrderInfo.status == '0020' ||
              getTripOrderInfo.status == '0020') {
            // 当前现实目标名称
            currentDisplayTarget = getTripOrderInfo.destArea;
            // 当前现实目标位置
            currentDestination = {
              'latitude': getTripOrderInfo.destLatitude,
              'longitude': getTripOrderInfo.destLongitude,
            };
          }
          notifyListeners();
        });
      }
    });
  }

  /// 跳转订单进行中
  // judgeJumpCurrentOrder(context) {
  //   /// 获取订单信息
  //   AppGlobalState providerAppGlobalState = Provider.of(context);
  //   providerAppGlobalState.setWorkimgStatus(context, '0020', '2');
  // }

  /// 订单跳转
  ///
  // jumpCurrentOrder(context) {
  //   switch (transferStatus) {
  //     case 0: // 为去接乘客
  //       transferStatus = 0;
  //       NavigatorUtil.jump(context, '/pickUpPassengers');
  //       break;
  //     case 1: // 为到达约定地点
  //       NavigatorUtil.jump(context, '/pickUpPassengers');
  //       break;
  //     case 2: // 还未到达目的地
  //       NavigatorUtil.jump(context, '/pickUpPassengers');
  //       break;
  //     default:
  //   }
  // }

  setTransferStatus(data) {
    transferStatus = data;
    notifyListeners();
  }
}
